/*
* pps_bom
*/
package com.pitop.product.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ProductException;
import com.pitop.core.tools.StringTools;
import com.pitop.product.api.ProductFeignApi;
import com.pitop.product.entity.Product;
import com.pitop.product.entity.ProductState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 成品
 *
 * @author berton
 */
@Service("productSVImpl")
@Slf4j
public class ProductSVImpl {

    @Autowired
    private ProductFeignApi productFeignApi;

    /**
     * 加载一个对象Product
     *
     * @param id * @param code 编码
     * @return Product
     */
    public Product load(Long id, String code) {
        if (id == null && code == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return productFeignApi.load(id, code);
    }

    /**
     * 加载一个对象Product 通过id,(将查询关联数据)
     *
     * @param id
     * @return Product
     */
    public Product loadById(Long id) {
        if (id == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return productFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Product 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Product
     */
    public Product loadByCode(String code) {
        if (code == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return productFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象Product,(将查询关联数据)
     *
     * @param id * @param code 编码
     * @return Product
     */
    public Product get(Long id, String code) {
        if (id == null && code == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return productFeignApi.get(id, code);
    }

    /**
     * 加载一个对象Product 通过id,(将查询关联数据)
     *
     * @param id
     * @return Product
     */
    public Product getById(Long id) {
        if (id == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return productFeignApi.getById(id);
    }

    /**
     * 加载一个对象Product 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Product
     */
    public Product getByCode(String code) {
        if (code == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return productFeignApi.getByCode(code);
    }

    /**
     * 删除对象Product
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        productFeignApi.delete(id, code);
    }

    /**
     * 查询Product分页
     *
     * @param product  对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Product>
     */
    public List<Product> list(Product product, int curPage, int pageSize) {
        return productFeignApi.list(product, curPage, pageSize);
    }

    /**
     * 查询Product分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Product>
     */
    public List<Product> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Product product = new Product();
        product.setId(id);
        product.setCode(code);

        return productFeignApi.list(product, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return productFeignApi.count(id, code);
    }

    public int count(Product product) {
        return productFeignApi.count(product);
    }

    /**
     * 保存
     * 1.参数合法性验证
     * 2.SKU不能重复
     * 3.upc不能重复
     *
     * @param product 实体
     * @throws BaseException
     */
    public void save(Product product) {
        //1.参数合法性验证
        if (product == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //2.SKU不能重复
        Product productFlag = productFeignApi.loadBySku(product.getSku());
        if (productFlag != null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Product_Sku_Exist);
        }
        //3.upc不能重复
        if (StringTools.isNotEmpty(product.getUpc())) {
            productFlag = productFeignApi.loadByUpc(product.getUpc());
            if (productFlag != null) {
                throw new ProductException(BaseException.BaseExceptionEnum.Product_Upc_Exist);
            }
        }
        productFeignApi.build(product);
    }

    /**
     * 更新
     * 1.参数合法性验证
     * 2.SKU不能重复
     * 3.upc不能重复
     *
     * @param product 实体
     * @throws BaseException
     */
    public void modify(Product product) {
        //1.参数合法性验证
        if (product == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //2.SKU不能重复
        Product productFlag = productFeignApi.loadBySku(product.getSku());
        if (productFlag != null && !product.getCode().equals(productFlag.getCode())) {
            throw new ProductException(BaseException.BaseExceptionEnum.Product_Sku_Exist);
        }
        //3.upc不能重复
        if (StringTools.isNotEmpty(product.getUpc())) {
            productFlag = productFeignApi.loadByUpc(product.getUpc());
            if (productFlag != null && !product.getCode().equals(productFlag.getCode())) {
                throw new ProductException(BaseException.BaseExceptionEnum.Product_Upc_Exist);
            }
        }
        productFeignApi.modify(product);
    }

    /**
     * 修改成品状态
     *
     * @param code  成品编码
     * @param state 状态
     */
    public void updateState(String code, ProductState state) {
        if (StringTools.isEmpty(code) || state == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Product product = productFeignApi.loadByCode(code);
        if(product == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        productFeignApi.updateState(code, state);
    }


    /**
     * 复制成品
     *
     * @param code 编码
     */

    public void copy(String code) {
        if (StringTools.isEmpty(code)) {
            throw new ProductException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Product product = productFeignApi.loadByCode(code);
        if(product == null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        String sku = product.getSku() + "_copy";
        Product productFlag = productFeignApi.loadBySku(sku);
        if(productFlag != null) {
            throw new ProductException(BaseException.BaseExceptionEnum.Product_Has_Been_Copy);
        }
        if(StringTools.isNotEmpty(product.getUpc())) {
            String upc = product.getUpc() + "_copy";
            productFlag = productFeignApi.loadByUpc(upc);
            if(productFlag != null) {
                throw new ProductException(BaseException.BaseExceptionEnum.Product_Has_Been_Copy);
            }
        }
        productFeignApi.copy(code);
    }

}
